using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using logistical.Models;

namespace logistical.Controllers
{
    public class base_employeeController : baseController
    {
        public class list_base_employee
        {
            public List<base_employee> objs { get; set; }
        }

        [HttpGet]
        public object get()
        {
            try
            {
                var o = new logisticalEntities();
                var p = o.base_employee;
var dept = o.base_dept.Select(x => new
                                {
                                    id = x.id,
                                    text = x.name
                                });
                                
                return new { 
                                data = p
,dept
                            };
            }
            catch (Exception ex)
            {
                return new { msg = ex.Message };
            }
        }


        [HttpPost]
        public object add(base_employee obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                obj.id = Guid.NewGuid().ToString();
                common.sysEval.evalCurUserAndDate(obj);
                o.base_employee.Add(obj);
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object edit(base_employee obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.base_employee.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.base_employee.SingleOrDefault(x => x.id == obj.id);
old.code=obj.code;
old.name=obj.name;
old.cardid=obj.cardid;
old.mobile=obj.mobile;
old.dept=obj.dept;
                    common.sysEval.evalCurUserAndDate(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object del(base_employee obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.base_employee.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.base_employee.SingleOrDefault(x => x.id == obj.id);
                    o.base_employee.Remove(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object mdel(list_base_employee objs)
        {
            string msg = "";
            try
            {
            var o = new logisticalEntities();
            foreach (var p in objs.objs)
            {
                var co = o.base_employee.Where(x => x.id == p.id);
                if (co.Count() == 1)
                {
                    var old = o.base_employee.SingleOrDefault(x => x.id == p.id);
                    o.base_employee.Remove(old);
                }
            }
            o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }
    }
}
